package com.example.demo.ChunkServer;

import com.example.demo.Entity.Chunk;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * 块服务器逻辑
 * @author xyt
 */

public class ServerBase {
  private String serverName;
  private ArrayList<Chunk> chunkList;
  private HashMap<Long,String> localChunkHashMap;

  public HashMap<Long, String> getLocalChunkHashMap() {
    return localChunkHashMap;
  }

  public void setLocalChunkHashMap(HashMap<Long, String> localChunkHashMap) {
    this.localChunkHashMap = localChunkHashMap;
  }


  public ServerBase(String serverName){
    this.serverName = serverName;
    this.chunkList = new ArrayList<Chunk>();
  }

  public void updateHash(){
    UpdateHashThread updateHashThread = new UpdateHashThread("/" + serverName + "/chunk_",getChunkIdList(),localChunkHashMap);
    updateHashThread.start();
  }

  public synchronized String getChunkFilePath(long chunkId) {
    return "/" + serverName + "/chunk_" + chunkId;//存储在服务器系统中具体的块文件
  }


  public synchronized LinkedList<Long> getChunkIdList(){
    LinkedList<Long> chunkIdList = new LinkedList<Long>();
    int len = chunkList.size();
    for(int i=0;i < len;i++){
      chunkIdList.add(Long.valueOf(chunkList.get(i).getChunkId()));
    }

    return chunkIdList;
  }
}
